﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.WebModel;
using TrainingsApp.Logic;
using TrainingsApp.Model;
using TrainingsApp.IPADApp.UserControls.Main;
using ZJFramework10.Extension;
using TrainingsApp.WebModel.Route;
namespace TrainingsApp.IPADApp.UserControls2.Teacher {
    public partial class UC_Register : AccountControl {
        protected void Page_Load(object sender, EventArgs e) {

            this.dropYear.DataSource = StaticDataSource.ListYear();
            this.dropYear.DataTextField = "Text";
            this.dropYear.DataValueField = "Value";
            this.dropYear.DataBind();

            this.dropMonth.DataSource = StaticDataSource.ListMonth();
            this.dropMonth.DataTextField = "Text";
            this.dropMonth.DataValueField = "Value";
            this.dropMonth.DataBind();

            this.dropDay.DataSource = StaticDataSource.ListDay();
            this.dropDay.DataTextField = "Text";
            this.dropDay.DataValueField = "Value";
            this.dropDay.DataBind();

            this.dropPosition.DataSource = TrainingsApp.Logic.TS_School_Position_Logic.TS_School_Position_ListByPaging(1000, 1).TS_School_Position_List;
            this.dropPosition.DataTextField = "PositionName";
            this.dropPosition.DataValueField = "Idx";
            this.dropPosition.DataBind();

            TS_User_Entity entity = null;
            if (TemplateUserInfoManager.TryGet(out entity)) {
                dropGrade.DataSource = TrainingsApp.Logic.TS_Class_Logic.TS_Class_ListGrade(entity.SchoolId);
                dropGrade.DataTextField = "GradeName";
                dropGrade.DataValueField = "Idx";
                dropGrade.DataBind();
            }


            this.dropCourse.DataSource = TrainingsApp.Logic.TS_CourseInfo_Logic.TS_CourseInfo_ListByPaging(100, 1).TS_CourseInfo_List;
            this.dropCourse.DataTextField = "CourseName";
            this.dropCourse.DataValueField = "Idx";
            this.dropCourse.DataBind();



        }


        protected void btnSubmit_Click(object sender, EventArgs args) {

            int classId = ZJFramework10.Utility.FormManager.GetValue<int>("dropClass", 0);


            int courseId = this.dropCourse.SelectedValue.AsInt();
            int jiaocaiId = ZJFramework10.Utility.FormManager.GetValue<int>("dropJiaoCai", 0);
            TS_User_Entity userEntity = null;
            TemplateUserInfoManager.TryGet(out userEntity);


            TS_User_Teacher_Entity teacher = new TS_User_Teacher_Entity();
            teacher.UserName = userEntity.UserName;
            teacher.UserType = (int)userEntity.UserType;
            teacher.SchoolId = userEntity.SchoolId;
            if (this.dropPosition.SelectedIndex > 0)
                teacher.PositionId = this.dropPosition.SelectedValue.AsInt();
            teacher.Password = userEntity.Password;
            teacher.Name = this.txtName.Text;
            teacher.Mobile = userEntity.Mobile;
            teacher.ManagerStatus = 0;
            teacher.Email = userEntity.Email;
            teacher.ClassesId = classId;
            teacher.BirthDay = new DateTime(this.dropYear.SelectedValue.AsInt(), this.dropMonth.SelectedValue.AsInt(), this.dropDay.SelectedValue.AsInt());
            teacher.CourseId = courseId;
            teacher.MaterialsId = jiaocaiId;
            teacher.UserSource = UserSources.Register;
            teacher = TrainingsApp.Logic.TS_User_Teacher_Logic.TS_User_Teacher_Add(new List<TS_User_Teacher_Entity> { 
             teacher
            });

            if (teacher.UserId > 0) {

                var user = Sys_Permission_Logic.GetUserInfo(teacher.UserName);
                TemplateUserInfoManager.Clear();
                Session["CurContext"] = user;

                Response.Redirect(this.CurPage.RouteHelper.Route(RouteNames.Success, new { code = SuccessCode.RegisterSuccess }));
            }
        }
    }
}